import java.util.Locale;


public class Node 
{
	public static int unitCounter;
	public Node childUnit;
	public double data;
	
	private Node firstUnit;
	
	public Node()
	{
		// Dezimal "." statt ","
		Locale.setDefault(Locale.ENGLISH);
		
		unitCounter++;
		
		if (unitCounter == 1)
			firstUnit = this;
	}

	public static Object remove(Node warteschlange) 
	{
		if (unitCounter == 0)
		{
			warteschlange.data = 0;
			warteschlange.firstUnit = null;
			warteschlange.childUnit = null;
		}
		
		
		double tempData;
		if (warteschlange.firstUnit != null)
		{
			tempData = warteschlange.firstUnit.data;
			warteschlange.firstUnit = warteschlange.firstUnit.childUnit;
		}
		else
			return null;
		
		System.gc();
		unitCounter--;
		return tempData;
	}

	
	public static void insert(Node warteschlange, double data) 
	{
		if (unitCounter == 0)
		{
			warteschlange.data = data;
			warteschlange.firstUnit = warteschlange;
			unitCounter++;
			return;
		}
		
		Node lastUnit = warteschlange;
		while (hasNext(lastUnit)) 
		{
			lastUnit = lastUnit.childUnit;
		}
		
		lastUnit.childUnit = new Node();
		lastUnit.childUnit.data = data;
	}

	public static Object peek(Node warteschlange) 
	{
		if (warteschlange == null)
			return null;
		
		return warteschlange.firstUnit.data;
	}

	
	public static boolean hasNext(Node unit) {
		return unit.childUnit != null;
	}
	
	public String toString()
	{
		if (unitCounter == 0) return "Leer";
		
		StringBuilder builder = new StringBuilder();
		
		Node unit = firstUnit;
		while (hasNext(unit)) {
			builder.append(unit.data + " ");
			unit = unit.childUnit;
		}
		
		builder.append(unit.data);
		
		return builder.toString();
	}
	
}
